home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / xblockbuster / score.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  5.5 KB  |  206 lines

  1. /*
  2.  * File:       score.c
  3.  * Author:     Eric Van Gestel
  4.  * Updated for X11 by Mark S. Wedel
  5.  *
  6.  * For:                xblockbuster
  7.  *
  8.  * Implementation:
  9.  *     Upto USER_SCORES scores are logged per user.
  10.  *     The insertion low point (i.e., the entry that will disappear) is defined
  11.  *     to be either the user's previous save entry, or his USER_SCORES'th entry
  12.  *     or the last entry if neither exists.
  13.  *     Sometimes saved scores may not be completed
  14.  *     (e.g, if the game crashed or if the player Quit explicitly).
  15.  *     Such entries are given a negative number of balls when detected
  16.  *     (criterium: score not bettered).
  17.  */
  18.  
  19. #include "xblockbuster.h"
  20.  
  21. struct Score {
  22.     int             score, balls_left;
  23.     char            login[9];
  24. }               score_board[NB_SCORES];
  25.  
  26. void center_text(offset, message )
  27. int offset;
  28. char *message;
  29. {
  30.         XDrawImageString(display, win, gc, 
  31.             ( STAGE_WIDTH_IN_PIXELS - strlen( message ) * font_width ) / 2,
  32.                 BORDER + ( 4 + offset ) * font_height, 
  33.                 message, strlen(message));
  34. }
  35.  
  36.  
  37. void
  38. show_score_board(  )
  39. {
  40.     register int    lows, s, ns,scorenum=0;
  41.     register int    update = FALSE;
  42.     register int    made_it = FALSE;
  43.     FILE           *fd;
  44.     char            buf[PATH_LENGTH];
  45.  
  46.     XClearWindow(display,win);
  47.  
  48.     /* initalize score board (in case it would be empty) */
  49.     for ( s = 0; s < NB_SCORES; s++ ) {
  50.         score_board[s].score = 100;
  51.         score_board[s].balls_left = 0;
  52.         ( void ) strcpy( score_board[s].login, "computer" );
  53.     }
  54.  
  55.     /* read the score file into the array */
  56.     sprintf( buf, SCOREFILE, playground );
  57.     if ( !( fd = fopen( buf, "r+" ) ) ) {
  58.         perror( "No score file" );
  59.         exit( 1 );
  60.     }
  61.     for ( s = 0; s < NB_SCORES; s++ ) {
  62.         fscanf( fd, "%d:%d:%s\n", &score_board[s].score,
  63.             &score_board[s].balls_left,
  64.             score_board[s].login );
  65.     }
  66.  
  67.     /* find insertion low point */
  68.     for ( lows = ns = 0; lows < NB_SCORES - 1; lows++ ) {
  69.         if ( !strcmp( score_board[lows].login, login ) ) {
  70.             if ( score_board[lows].balls_left > 0 ) {
  71.                 if ( score_board[lows].score > score ) {
  72.                     /*
  73.                      * an older incomplete game: complete
  74.                      * it
  75.                      */
  76.                     score_board[lows].balls_left *= -1;
  77.                     ns++;
  78.                     update = TRUE;
  79.                 } else
  80.                     /* (presumably) the previous save */
  81.                     break;
  82.             } else
  83.                 /* an older completed game */
  84.                 ns++;
  85.             if ( ns >= USER_SCORES )
  86.                 break;
  87.         }
  88.     }
  89.  
  90.     /* find insertion high point */
  91.     for ( s = 0; s <= lows && score_board[s].score > score; s++ );
  92.  
  93.     /* check to see if current score made it */
  94.     if ( s <= lows ) {
  95.         /* yes it did , so shift smaller scores */
  96.         for (  /* lows = lows */ ; lows > s; lows-- )
  97.             score_board[lows] = score_board[lows - 1];
  98.         score_board[s].score = score;
  99.         score_board[s].balls_left = balls_left;
  100.         ( void ) strcpy( score_board[s].login, login );
  101.         update = TRUE;
  102.         made_it = TRUE;
  103.         scorenum=s;
  104.     }
  105.     /* write updated score board */
  106.     if ( update ) {
  107.         rewind( fd );
  108.         for ( s = 0; s < NB_SCORES; s++ ) {
  109.             fprintf( fd, "%d:%d:%s\n", score_board[s].score,
  110.                  score_board[s].balls_left,
  111.                  score_board[s].login );
  112.         }
  113.     }
  114.     fclose( fd );
  115.  
  116.     /* show score board */
  117.     center_text( 0, "Top Scores" );
  118.     for ( s = 0; s < NB_SCORES; s++ ) {
  119.         if ( score_board[s].balls_left > 99 ) {
  120.             sprintf( buf, "%7d (%3d)   %8s ",
  121.                          score_board[s].score,
  122.                          score_board[s].balls_left,
  123.                          score_board[s].login );
  124.         } else if ( score_board[s].balls_left > 9 ) {
  125.             sprintf( buf, "%7d  (%2d)   %8s ",
  126.                          score_board[s].score,
  127.                          score_board[s].balls_left,
  128.                          score_board[s].login );
  129.         } else if ( score_board[s].balls_left > 0 ) {
  130.             sprintf( buf, "%7d   (%1d)   %8s ",
  131.                          score_board[s].score,
  132.                          score_board[s].balls_left,
  133.                          score_board[s].login );
  134.         } else if ( score_board[s].balls_left < -99 ) {
  135.             sprintf( buf, "%7d (%3d) + %8s ",
  136.                          score_board[s].score,
  137.                          -score_board[s].balls_left,
  138.                          score_board[s].login );
  139.         } else if ( score_board[s].balls_left < -9 ) {
  140.             sprintf( buf, "%7d  (%2d) + %8s ",
  141.                          score_board[s].score,
  142.                          -score_board[s].balls_left,
  143.                          score_board[s].login );
  144.         } else if ( score_board[s].balls_left < 0 ) {
  145.             sprintf( buf, "%7d   (%1d) + %8s ",
  146.                          score_board[s].score,
  147.                          -score_board[s].balls_left,
  148.                          score_board[s].login );
  149.         } else {    /* no balls left */
  150.             sprintf( buf, "%7d         %8s ",
  151.                          score_board[s].score,
  152.                          score_board[s].login );
  153.         }
  154.         center_text( s + 2, buf );
  155.  
  156.         if ((s==scorenum) && made_it)
  157.             XDrawImageString(display,win,gc,
  158.             (STAGE_WIDTH_IN_PIXELS - strlen(buf)*font_width)/2 
  159.             -4*font_width,
  160.             BORDER+ (6+s)*font_height, ">>> ",4);
  161.     }
  162.             
  163.  
  164.  
  165.  
  166.     /* make sure the current score is on the board */
  167.     if ( !made_it ) {
  168.         if ( balls_left > 99 ) {
  169.             sprintf( buf, "%7d (%3d)   %8s ",
  170.                            score, balls_left, login );
  171.         } else if ( balls_left > 9 ) {
  172.             sprintf( buf, "%7d  (%2d)   %8s ",
  173.                            score, balls_left, login );
  174.         } else if ( balls_left ) {
  175.             sprintf( buf, "%7d   (%1d)   %8s ",
  176.                            score, balls_left, login );
  177.         } else {    /* no balls left */
  178.             sprintf( buf, "%7d    <>   %8s ",
  179.                              score, login );
  180.         }
  181.         center_text( NB_SCORES + 3, buf );
  182.     }
  183.  
  184.     /* show the current pallet shrinkage */
  185.     sprintf( buf, "pallet shrinkage >>> %2d %% <<<",
  186.                 ( pallet_modif * 100 ) / PALLET_DENOMINATOR );
  187.     center_text( NB_SCORES + 5, buf );
  188.  
  189.     center_text( NB_SCORES + 8, "[ Click to exit ]");
  190.  
  191.  
  192.     XFlush(display);
  193.  
  194.     /* provide some time to read; wait for a button click */
  195.     {
  196.         XEvent    e;
  197.  
  198.         do {
  199.             XNextEvent(display, &e);
  200.         } while (e.type != ButtonPress);
  201.     }
  202.  
  203.     XCloseDisplay(display);
  204.     exit( 0 );        /* BYE !! */
  205. }
  206.